import junit.framework.TestCase;

public class JUnitMatrixTest extends TestCase {

	public static void main(String[] args) {
		junit.textui.TestRunner.run(JUnitMatrixTest.class);
	}

	/** Test method for 'Matrix.load_Matrix(String)'*/
	public void testLoad_Matrix()
	{
		try
		{
			Matrix ne = new MatrixList();
			Matrix a = ne.load_Matrix("./test/matrix/input_list.txt");
			Matrix b = ne.load_Matrix("./test/matrix/input_array.txt");
			//Test load_Matrix method
			assertEquals(a.getRows(),b.getRows());
			assertEquals(a.getCols(),b.getCols());
			for(int i = 0; i < a.getRows(); i++)
			{
				for(int j = 0; j < a.getCols(); j++)
				{
					assertEquals(a.getCell(i,j),b.getCell(i,j));
				}
			}
			//Test Compare Method
			assertTrue(a.compare_matrices(b));
			//Test Add Method
			Matrix added = a.add_matrices(a);
			for(int i = 0; i < a.getRows(); i++)
			{
				for(int j = 0; j < a.getCols(); j++)
				{
					assertEquals(added.getCell(i,j),a.getCell(i,j)+a.getCell(i,j));
				}
			}
			//Test Subtract Method
			Matrix subtract = a.subtract_matrices(a);
			for(int i = 0; i < a.getRows(); i++)
			{
				for(int j = 0; j < a.getCols(); j++)
				{
					assertEquals(subtract.getCell(i,j),a.getCell(i,j)-a.getCell(i,j));
				}
			}
		}
		catch(Exception e)
		{
			System.err.println("Error in test 'testLoad_Matrix': " + e);
		}
	}
	
	/** Test method for 'Matrix.add(Matrix)'*/
	public void testMultiply()
	{
		Matrix s = new MatrixArray();
		Matrix m1 = s.load_Matrix("./test/matrix/mul1.txt");
        Matrix m2 = m1.load_Matrix("./test/matrix/mul2.txt");
        Matrix ms = m1.load_Matrix("./test/matrix/muls.txt"); //Right Answer      
        Matrix m3 = m1.multiply(m2);
        
        assertEquals(m3.compare_matrices(ms)==true,true);
        
        Matrix l = new MatrixList();
		Matrix l1 = l.load_Matrix("./test/matrix/sparsemul1.txt");
        Matrix l2 = l1.load_Matrix("./test/matrix/sparsemul2.txt");
        Matrix ls = l1.load_Matrix("./test/matrix/sparsemuls.txt"); //Right Answer      
        Matrix l3 = l1.multiply(l2);
        
        assertEquals(l3.compare_matrices(ls)==true,true);
	}

	/** Test method for 'Matrix.add(Matrix)'*/
	public void testadd()
	{
		try
		{     
	        Matrix a = new MatrixList();
			Matrix a1 = a.load_Matrix("./test/matrix/sparseadd1.txt");
	        Matrix a2 = a1.load_Matrix("./test/matrix/sparseadd2.txt");
	        Matrix as = a1.load_Matrix("./test/matrix/sparseadds.txt"); //Right Answer      
	        Matrix a3 = a1.add_matrices(a2);
	        
	        assertEquals(a3.compare_matrices(as)==true,true);
	        
		}
		catch(Exception e)
		{
			System.err.println("Error in test 'testLoad_Matrix': " + e);
		}
	}

	/** Test method for 'Matrix.transpose(Matrix)' */
	public void testTranspose()
	{
		Matrix s = new MatrixArray();
		Matrix trans1 = s.load_Matrix("./test/matrix/trans.txt");
        Matrix trans_ans = trans1.load_Matrix("./test/matrix/transs.txt");
        Matrix calctrans = trans1.transpose();
        assertEquals(calctrans.compare_matrices(trans_ans)==true,true);
        
        //Matrix v = new MatrixList();
		//Matrix tran = v.load_Matrix("./test/matrix/sparsetrans.txt");
        //Matrix trans = v.load_Matrix("./test/matrix/sparsetranss.txt");
        //Matrix calctran = tran.transpose();
        //assertEquals(calctran.compare_matrices(trans)==true,true);
 	}

	/** Test method for 'Matrix.determinant(Matrix)'*/
	public void testDeterminant()
	{
		Matrix s = new MatrixArray();
		Matrix d2 = s.load_Matrix("./test/matrix/det2.txt");
        double det2 = d2.determinant();
        assertEquals(det2,(double)18);
	}

	/** Test method for 'Matrix.inverse(Matrix)'*/
	public void testInverse()
	{
		Matrix s = new MatrixArray();
		Matrix inv = s.load_Matrix("./test/matrix/inv1.txt");
        Matrix invs = s.load_Matrix("./test/matrix/invs.txt");
        Matrix invs1 = inv.inverse();
        assertEquals(invs.compare_matrices(invs1)==true,true);        
	}

	/** Test method for 'Matrix.scalarmul(Matrix, double)' */
	public void testScalarmul()
	{
		Matrix s = new MatrixArray();
		Matrix input = s.load_Matrix("./test/matrix/input_array.txt");
		Matrix scalmul = input.scalarmul(3);
		//scalmul.print();
		assertEquals(scalmul.getRows(),input.getRows());
		assertEquals(scalmul.getCols(),input.getCols());
		for(int i = 0; i < scalmul.getRows(); i++)
		{
			for(int j = 0; j < scalmul.getCols(); j++)
			{
				assertEquals(scalmul.getCell(i,j),(input.getCell(i,j)*3));
			}
		}
	}

	/** Test method for 'Matrix.getCell(int, int)'*/
	public void testGetCell()
	{
		Matrix s = new MatrixArray();
		s = s.load_Matrix("./test/matrix/input_array.txt");
		assertEquals(s.getCell(0,0),(double)1);
		
		s = new MatrixList();
		s = s.load_Matrix("./test/matrix/input_list.txt");
		assertEquals(s.getCell(0,0),(double)1);
	}

	/** Test method for 'Matrix.setCell(int, int, double)'*/
	public void testSetCell()
	{
		Matrix s = new MatrixArray(2,2);
		s.setCell(1,1,3.4);
		assertEquals(s.getCell(1,1),(double)3.4);
		
		s = new MatrixList(2,2);
		s.setCell(1,1,3.4);
		assertEquals(s.getCell(1,1),(double)3.4);
	}

	/** Test method for 'Matrix.deleteCell(int, int)'*/
	public void testDeleteCell()
	{
		Matrix s = new MatrixList(2,2);
		s.setCell(1,1,3.4);
		s.deleteCell(1,1);
		assertEquals(s.getCell(1,1),0.0);
		
		s = new MatrixArray(2,2);
		s.setCell(1,1,3.4);
		s.deleteCell(1,1);
		assertEquals(s.getCell(1,1),0.0);
	}

	/** Test method for 'Matrix.getType()'*/
	public void testGetType()
	{
		Matrix s = new MatrixList();
		s = s.load_Matrix("./test/matrix/input_list.txt");
		assertEquals(s.getType(),"MatrixList");
		
		s = s.load_Matrix("./test/matrix/input_array.txt");
		assertEquals(s.getType(),"MatrixArray");
	}

}
